#!/bin/ksh
#
# Cleans up the GNOME Desktop user configuration files.  This
# will return the user to the default desktop configuration.
# Useful if the user's configuration has become corrupted.
# 
# By: Brian Cameron <Brian.Cameron@sun.com>

# The first argument can be a user name.  If so, then the script
# will clean up the files for that specified user (if file
# permissions permit).  If no argument is given, the default value
# is the current user.
#
if [ $# -ge 1 ]; then
  LOGNAME="$1"
  USRHOME=`echo ~$1`
else
  USRHOME="$HOME"
  if [ -z "$LOGNAME" ]; then
    LOGNAME=`/usr/bin/logname`
  fi
fi

# Error if the directory for this user does not exist.
#
if [ ! -d "$USRHOME" ]; then
   echo "\nError: user <$LOGNAME> does not exist on this system.\n"
   exit 1
fi

# If USRHOME is the root directory, just set USRHOME to nothing
# to avoid double-slash in the output since we refer to files
# as $USRHOME/.gconf, for example.
#
if [ "$USRHOME" = "/" ]; then
   USRHOME=""
fi

# Check if GNOME is running:
#
GNOME_PROCESSES='(gnome-session|gconfd|gconfd-2|metacity|esd)'
RUNNING_PROCESSES=`/usr/bin/pgrep -l -U $LOGNAME "$GNOME_PROCESSES"`
rc=$?
if [ $rc -ge 2 ]; then
   echo "\nError getting user process information for user <$LOGNAME>...\n"
   exit 1
fi

if [ ! -z "$RUNNING_PROCESSES" ]; then
   echo "\nThe following GNOME processes are still running for user <$LOGNAME>:\n"
   echo "$RUNNING_PROCESSES"
   echo "\nPlease log out user <$LOGNAME> from GNOME, so this user has no"
   echo "GNOME processes running before using gnome-cleanup.  For example,"
   echo "log out, and log into a failsafe session to run gnome-cleanup."
   echo ""
   exit 1
fi

# Use disp_files to echo files back to the screen so that we don't expand
# "tmp" wildcard directories like gvfs-${LOGNAME}, otherwise the output
# is cumbersome to read since this will echo dozens of files to the screen.
#
disp_files=""

# GNOME 2.x files
#
gnome_files="$USRHOME/.dbus $USRHOME/.gconf $USRHOME/.gconfd $USRHOME/.gnome $USRHOME/.gnome-desktop $USRHOME/.gnome2 $USRHOME/.gnome2_private $USRHOME/.metacity $USRHOME/.nautilus $USRHOME/.esd_auth $USRHOME/.gtkrc $USRHOME/.gtkrc-1.2-gnome2 $USRHOME/.nautilus-metafile.xml $USRHOME/.gstreamer-0.10/registry.* $USRHOME/.local/share $USRHOME/.config"

# GNOME 1.4 files
#
gnome_14_files="$USRHOME/.gnome-help-browser $USRHOME/.gnome_private $USRHOME/.thumbnails $USRHOME/Nautilus"

check_files=`/bin/ls -d $gnome_files $gnome_14_files 2> /dev/null`
if [ ! -x "$check_files" ]
then
   disp_files="$disp_files\n$check_files"
fi

# tmp files
#
tmp_dirs="/var/tmp $TEMPDIR $TMP $TEMP"
tmp_files=""

tmp_cleanup="gconfd-${LOGNAME} mapping-${LOGNAME} orbit-${LOGNAME} gvfs-${LOGNAME}*"

for dir in $tmp_dirs; do
   for cleanup in $tmp_cleanup; do
      tmp_files="$dir/$cleanup $tmp_files"

      check_files=`/bin/ls -d $dir/$cleanup 2> /dev/null`
      if [ ! -x "$check_files" ]
      then
         disp_files="$disp_files\n$dir/$cleanup"
      fi
   done
done
   
has_files=`/bin/ls -d $tmp_files $gnome_files $gnome_14_files 2> /dev/null`
has_user_files=`/bin/ls -d $gnome_files $gnome_14_files 2> /dev/null`

if [ ! -z "$has_files" ]
then
   echo "\nUser <$LOGNAME> currently has the following GNOME configuration files:"
   echo "$disp_files"
   echo "\nDo you wish to remove these files (Y/N) \c"
   read input;

   if [ "$input" = "Y" -o "$input" = "y" ]
   then
      # Back up files for debugging.
      date=`/usr/bin/date +%F-%H:%M:%S`
      tarfile="/tmp/gnome-cleanup-$LOGNAME-$date.tar"
      /usr/bin/tar -cf $tarfile $has_user_files

      /bin/rm -fR $has_files
      rc=$?
      if [ $rc = 0 ]; then
         echo "\nRemoved..."
         echo "\nThe files removed from the user \$HOME directory have been"
         echo "backed up to the following file:"
         echo "\n$tarfile"
         echo "\nIf you ran this program to resolve an issue that was caused"
         echo "by the GNOME desktop not functioning properly, then please"
         echo "report a bug at http://defect.opensolaris.org/."
         echo "\nNote that configuration files may contain sensitive"
         echo "information about you.  If this is a concern, then it is best"
         echo "to not attach this tar file to a publicly visible bug report."
         echo "If this is not an issue, then attaching this file would likely"
         echo "help to debug the problem.  Also note that files in the /tmp"
         echo "directory are removed automatically on reboot, so move this"
         echo "file to a more permanent location if you want to save it for"
         echo "reference or future use."
      else
         echo "Error removing files..."
      fi
   else
      echo "Not removed..."
   fi
   echo ""
else
   echo "\nUser $LOGNAME does not have any GNOME configuration files.\n"
fi
